﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication
{
    public class Example
    {
        public static void Main()
        {
            List<string> dinosaurs = new List<string>();

            dinosaurs.Add("Compsognathus");
            dinosaurs.Add("Amargasaurus");
            dinosaurs.Add("Oviraptor");
            dinosaurs.Add("Velociraptor");
            dinosaurs.Add("Deinonychus");
            dinosaurs.Add("Dilophosaurus");
            dinosaurs.Add("Gallimimus");
            dinosaurs.Add("Triceratops");

            Console.WriteLine();
            foreach(string dinosaur in dinosaurs)
            {
                Console.WriteLine(dinosaur);
            }

            Console.WriteLine("\nTrueForAll(EndsWithSaurus): {0}",
                dinosaurs.TrueForAll(EndsWithSaurus));

            Console.WriteLine("\nFind(EndsWithSaurus): {0}", 
                dinosaurs.Find(EndsWithSaurus));

            Console.WriteLine("\nFindLast(EndsWithSaurus): {0}",
                dinosaurs.FindLast(EndsWithSaurus));

            Console.WriteLine("\nFindAll(EndsWithSaurus):");
            List<string> sublist = dinosaurs.FindAll(EndsWithSaurus);

            foreach(string dinosaur in sublist)
            {
                Console.WriteLine(dinosaur);
            }

            Console.WriteLine(
                "\n{0} elements removed by RemoveAll(EndsWithSaurus).", 
                dinosaurs.RemoveAll(EndsWithSaurus));

            Console.WriteLine("\nList now contains:");
            foreach(string dinosaur in dinosaurs)
            {
                Console.WriteLine(dinosaur);
            }

            Console.WriteLine("\nExists(EndsWithSaurus): {0}", 
                dinosaurs.Exists(EndsWithSaurus));

            ListString();
            ListHM();
            ListHMnew();

            Console.Read();
        }

        // Search predicate returns true if a string ends in "saurus".
        private static bool EndsWithSaurus(String s)
        {
            Console.WriteLine("这是S:"+s);
            if ((s.Length > 5) && 
                (s.Substring(s.Length - 6).ToLower() == "saurus"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        private static void ListString()
        {
            string model;
            List<string> List = new List<string>();
            for (var i = 0; i < 5; i++)
            {
                model= i.ToString();
                List.Add(model);
            }
            List.TrueForAll(S => { Console.WriteLine("S:" + S); return true; });
        }

        private static void ListHM()
        {
            HM model = new HM();
            List<HM> List = new List<HM>();
            for (var i = 0; i < 5; i++)
            {
                model.ID = i;
                List.Add(model);
            }
            List.TrueForAll(S => { Console.WriteLine("HM is S:" + S.ID); return true; });
        }

        private static void ListHMnew()
        {
            HM model;
            List<HM> List = new List<HM>();
            for (var i = 0; i < 5; i++)
            {
                model = new HM();
                model.ID = i;
                List.Add(model);
            }
            List.TrueForAll(S => { Console.WriteLine("HM is new S:" + S.ID); return true; });
        }

    }
    public class HM 
    {
        public HM()
        { }
        public int ID { set; get; }
        public string Str { set; get; }
    }
}
